home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / COSEmulator / COSEmulator- SRC / src / MenuItemWindow.cpp < prev    next >
Encoding:
Text File  |  1998-06-21  |  10.9 KB  |  478 lines  |  [TEXT/CWIE]

  1. //===================================================================
  2. //=======================  Headers        =============================
  3.  
  4. #include "MenuItemWindow.h"
  5. #include "GameUtilities.h"
  6. #include "Screen.h"
  7. #include "ApplicationEvents.h"
  8. #include "ApplicationHandler.h"
  9.  
  10. #include <string.h>
  11.  
  12. //===================================================================
  13. //=======================  Globals        =============================
  14.  
  15. OffScreenBuff    menuStuff;
  16.  
  17. rect            rectMenuStuff[ 1 ];
  18.  
  19. //===================================================================
  20. //=======================  #define        =============================
  21.  
  22.  
  23. #define        kMenuLine        0
  24.  
  25. //===================================================================
  26. //=======================  Function Prototypes    =====================
  27.  
  28. /*----------------------------------------------------------------------------\
  29.  
  30.     InitMenuItemWindow
  31.  
  32. \----------------------------------------------------------------------------*/
  33. void    InitMenuItemWindow( void )
  34. {
  35.     if( !menuStuff.LoadPicBuff( 132 ) )
  36.     {
  37.         DebugStr("\p AAAAAhhhhhhhhhhh InitMenuItemWindow() ");
  38.         ExitToShell();
  39.     }
  40.     
  41.     MySetRect( &rectMenuStuff[ 0 ] , 0 , 0 , 200 , 20 );
  42. }
  43.  
  44. /*----------------------------------------------------------------------------\
  45.  
  46.     MenuItemWindow :: Constructor
  47.  
  48. \----------------------------------------------------------------------------*/
  49.     MenuItemWindow :: MenuItemWindow( void )
  50. {
  51.     numItems = 0;
  52.     shaded = true;
  53.     type = 0;
  54.     
  55.     short i;
  56.     
  57.     for( i = 0; i < kMaxMenuItemName; i++ )
  58.     {
  59.         items[ i ][ 0 ] = NULL;
  60.     }
  61.     
  62.     selectedItem = 0xff;
  63. }
  64.  
  65. /*----------------------------------------------------------------------------\
  66.  
  67.     MenuItemWindow :: SetMaxItems
  68.  
  69. \----------------------------------------------------------------------------*/
  70. void    MenuItemWindow :: SetMaxItems( uchar num )
  71. {
  72.     if( numItems != num )
  73.     {
  74.         if( numItems > 0 )
  75.         {
  76.             window.DeleteBuff();
  77.         }
  78.         
  79.         numItems = num;
  80.         
  81.         CalcWindowSize();
  82.         
  83.         if( numItems > 0 )
  84.         {    
  85.             if( !window.NewBuff( width , height ) )
  86.                 DebugStr( "\p Something went wrong MenuItemWindow :: SetMaxItems");
  87.                 
  88.             FirstDraw();                            // this is a hack tso this is ok 
  89.         }
  90.         
  91.     }
  92. }
  93.  
  94. /*----------------------------------------------------------------------------\
  95.  
  96.     MenuItemWindow :: AddItem
  97.  
  98. \----------------------------------------------------------------------------*/
  99. void    MenuItemWindow :: AddItem( uchar num , char *name , Boolean active )
  100. {
  101.     if( num + 1 > numItems )
  102.     {
  103.         SetMaxItems( num  + 1);
  104.     }
  105.     
  106.     if( name == NULL )
  107.     {
  108.         items[ num ][ 0 ] = NULL;
  109.         itemActive[ num ] = false;
  110.     }
  111.     else
  112.     {
  113.         strcpy( items[ num ] , name );
  114.         itemActive[ num ] = active;
  115.     }
  116.     
  117.     WriteName( num , false );
  118.     
  119. }
  120.  
  121. /*----------------------------------------------------------------------------\
  122.  
  123.     MenuItemWindow :: RemoveItem
  124.  
  125. \----------------------------------------------------------------------------*/
  126. void    MenuItemWindow :: RemoveItem( uchar num )
  127. {
  128.     if( num < numItems )
  129.     {
  130.         items[ num ][ 0 ] = NULL;
  131.     }
  132. }
  133.  
  134. /*----------------------------------------------------------------------------\
  135.  
  136.     MenuItemWindow :: MenuItemRect
  137.  
  138. - assume all the items have been added
  139.  
  140. \----------------------------------------------------------------------------*/
  141. void    MenuItemWindow :: MenuItemRect( const rect *menuItem )
  142. {    
  143.     MySetRectWH( &screenLoc , menuItem->left , menuItem->bottom , 
  144.                  width , height );
  145.     
  146.     if( !screen.RectAllInScreen( &screenLoc ) )
  147.     {
  148.         MySetRect( &screenLoc , menuItem->right - width ,(int)menuItem->bottom 
  149.                               , (int)menuItem->right , height + menuItem->bottom );
  150.     
  151.     }    
  152. }
  153.  
  154. /*----------------------------------------------------------------------------\
  155.  
  156.     MenuItemWindow :: SetType
  157.  
  158. \----------------------------------------------------------------------------*/
  159. void    MenuItemWindow :: SetType( uchar t )
  160. {
  161.     type = t;
  162. }
  163.  
  164. /*----------------------------------------------------------------------------\
  165.  
  166.     MenuItemWindow :: SetActive
  167.  
  168. \----------------------------------------------------------------------------*/
  169. void    MenuItemWindow :: SetActive( Boolean a )
  170. {
  171.     if( shaded != !a )
  172.     {
  173.         shaded = !a;
  174.         
  175.         screen.AddRectToUpdate( screenLoc );
  176.         
  177.         if( shaded )
  178.         {
  179.             if( selectedItem != 0xff )
  180.             {
  181.                 WriteName( selectedItem , false );
  182.                 selectedItem = 0;
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. /*----------------------------------------------------------------------------\
  189.  
  190.     MenuItemWindow :: GetNumItems
  191.  
  192. \----------------------------------------------------------------------------*/
  193. uchar    MenuItemWindow :: GetNumItems( void )
  194. {
  195.     return numItems;
  196. }
  197.  
  198. /*----------------------------------------------------------------------------\
  199.  
  200.     MenuItemWindow :: Init
  201.  
  202. - this hsould be called after all window setts are set
  203. \----------------------------------------------------------------------------*/
  204. Boolean    MenuItemWindow :: Init( void )
  205. {    
  206.  
  207.     shaded = true;    
  208.     FirstDraw();
  209.     
  210.     return true;
  211. }
  212.  
  213. /*----------------------------------------------------------------------------\
  214.  
  215.     MenuItemWindow :: HandleMouseClick
  216.  
  217. \----------------------------------------------------------------------------*/
  218. void    MenuItemWindow :: HandleMouseClick( Boolean down , point where )
  219. {
  220.     if( shaded )
  221.         return;
  222.         
  223.     if( !SectPtRect( where , screenLoc ) )    
  224.         return;
  225.         
  226.     if( down )
  227.     {
  228.         
  229.     }
  230.     else
  231.     {
  232.         if( selectedItem != 0xff )
  233.         {
  234.             WriteName( selectedItem , false );
  235.             SendSelectEvent( selectedItem );
  236.             selectedItem = 0;
  237.         }
  238.     }
  239. }
  240.  
  241. /*----------------------------------------------------------------------------\
  242.  
  243.     MenuItemWindow :: HandleMouseMove
  244.  
  245. \----------------------------------------------------------------------------*/
  246. void    MenuItemWindow :: HandleMouseMove( point where )
  247. {
  248.     if( shaded )
  249.         return;
  250.  
  251.     if( !SectPtRect( where , screenLoc ) )    
  252.     {
  253.         if( selectedItem != 0xff )
  254.         {
  255.             WriteName( selectedItem , false );
  256.             AddItemToUpdate( selectedItem );
  257.             selectedItem = 0xff;
  258.         }
  259.         return;
  260.     }
  261. // find which one is selected and highlight
  262.     short    which;
  263.     
  264.     which = ( where.y - screenLoc.top ) / kMenuItemHeight;
  265.     
  266.     if( which != selectedItem && selectedItem != 0xff)
  267.     {
  268.         WriteName( selectedItem , false );
  269.         AddItemToUpdate( selectedItem );
  270.     }
  271.     
  272.     if( ((which != selectedItem) && itemActive[ which ]) && (items[ which ][ 0 ] != NULL) )
  273.     {
  274.         WriteName( which , true );
  275.         selectedItem = which;
  276.         AddItemToUpdate( which );
  277.     }
  278. }
  279.  
  280. /*----------------------------------------------------------------------------\
  281.  
  282.     MenuItemWindow :: PointInWindow
  283.  
  284. - just saids if the point it inside the window area or not
  285. \----------------------------------------------------------------------------*/
  286. Boolean    MenuItemWindow :: PointInWindow( point where )
  287. {
  288.     if( shaded )
  289.         return false;
  290.         
  291.     return SectPtRect( where , screenLoc );
  292. }
  293.  
  294. /*----------------------------------------------------------------------------\
  295.  
  296.     MenuItemWindow :: Active
  297.  
  298. \----------------------------------------------------------------------------*/
  299. Boolean    MenuItemWindow :: Front( void )
  300. {
  301.     return( front );
  302. }
  303.  
  304. /*----------------------------------------------------------------------------\
  305.  
  306.     MenuItemWindow :: SetFront
  307.  
  308. \----------------------------------------------------------------------------*/
  309. void    MenuItemWindow :: SetFront( Boolean f )
  310. {
  311.  
  312. }
  313.  
  314. /*----------------------------------------------------------------------------\
  315.  
  316.     MenuItemWindow :: DrawToScreen
  317.  
  318. \----------------------------------------------------------------------------*/
  319. void    MenuItemWindow :: DrawToScreen( rect *where , Boolean highlighted )
  320. {
  321.     if( shaded )
  322.         return;
  323.         
  324.     if( where == NULL )
  325.     {
  326.         screen.DrawGeneric( &window , &window.GetBoundsSize() , &screenLoc ,
  327.                             NULL , 0 , 0 , 0 );
  328.     }
  329.     else
  330.     {
  331.         screen.DrawGeneric( &window , &window.GetBoundsSize() , &screenLoc ,
  332.                     where , 0 , 0 , 0 );
  333.     }
  334. }
  335.  
  336. /*----------------------------------------------------------------------------\
  337.  
  338.     MenuItemWindow :: FirstDraw
  339.  
  340. - creates the menu list the first time with no thrils
  341.  
  342. \----------------------------------------------------------------------------*/
  343. void    MenuItemWindow :: FirstDraw( void )
  344. {    
  345.     // draw the colored back ground
  346.         
  347.     PaintRect( &window , &window.GetBoundsSize() , kMenuGrey );
  348.     
  349.     // write all the text names
  350.     
  351.     uchar i;
  352.     
  353.     for( i = 0; i < numItems; i++ )
  354.     {
  355.         WriteName( i , false );
  356.     }
  357. }
  358.  
  359. /*----------------------------------------------------------------------------\
  360.  
  361.     MenuItemWindow :: FirstDraw
  362.  
  363. - creates the menu list the first time with no thrils
  364.  
  365. \----------------------------------------------------------------------------*/
  366. void    MenuItemWindow :: WriteName( uchar which , Boolean    highlighted )
  367. {    
  368.     rect    loc;
  369.     RGBColor    oldColor;
  370.     RGBColor    grey;
  371.     CGrafPtr    oldCPort;
  372.     GDHandle    oldDevice;
  373.     
  374.     MySetRectWH( &loc , 0 , 20 * which , 200 , 20 );
  375.  
  376.     if( items[ which ][ 0 ] == NULL )
  377.     {
  378.          DrawPicture( &menuStuff , &rectMenuStuff[ kMenuLine ] , &window , &loc );
  379.     }
  380.     else if( itemActive[ which ] )
  381.     {
  382.         PaintRect( &window , &loc , kMenuGrey );
  383.         
  384.         grey.red = 0x3333;
  385.         grey.blue = 0x3333;
  386.         grey.green = 0x3333;
  387.         
  388.         GetGWorld( &oldCPort, &oldDevice );
  389.  
  390.         SetGWorld( (window.GetOffScreen())->GWorld , ( GDHandle )nil );
  391.         
  392.         GetForeColor( &oldColor );
  393.         
  394.         RGBForeColor( &grey );
  395.             
  396.             MoveTo( loc.left + 20, loc.bottom - 7 );
  397.             DrawString( c2pstr( items[ which ] ) );
  398.             p2cstr( (StringPtr)items[ which ] );
  399.         RGBForeColor( &oldColor );
  400.         
  401.         SetGWorld( oldCPort, oldDevice );
  402.         
  403.         if( highlighted )
  404.             DrawGeneric( &window , &loc , &window , &loc ,
  405.                      &window.GetBoundsSize() , kDrawCrop1 | kDrawInverse , 0 , 0x0000 );
  406.     }
  407.     else
  408.     {
  409.         PaintRect( &window , &loc , kMenuGrey );
  410.         
  411.         grey.red = 0x9999;
  412.         grey.blue = 0x9999;
  413.         grey.green = 0x9999;
  414.         
  415.         GetGWorld( &oldCPort, &oldDevice );
  416.  
  417.         SetGWorld( (window.GetOffScreen())->GWorld , ( GDHandle )nil );
  418.         
  419.         GetForeColor( &oldColor );
  420.         
  421.         RGBForeColor( &grey );
  422.             
  423.             MoveTo( loc.left + 20, loc.bottom - 7 );
  424.             DrawString( c2pstr( items[ which ] ) );
  425.             p2cstr( (StringPtr)items[ which ] );
  426.         RGBForeColor( &oldColor );
  427.         
  428.         SetGWorld( oldCPort, oldDevice );
  429.                      
  430.         //                  &window.GetBoundsSize() , kDrawCrop1  , 0 , 0x0000 );    
  431.     }
  432. }
  433.  
  434. /*----------------------------------------------------------------------------\
  435.  
  436.     MenuItemWindow :: CalcWindowSize
  437.  
  438. \----------------------------------------------------------------------------*/
  439. void    MenuItemWindow :: CalcWindowSize( void)
  440. {    
  441.     width = 200;
  442.     height = numItems * kMenuItemHeight;
  443.     
  444.     MySetRectWH( &screenLoc , screenLoc.left , screenLoc.top , 
  445.                  width , height );
  446. }
  447.  
  448. /*----------------------------------------------------------------------------\
  449.  
  450.     MenuItemWindow :: SendSelectEvent
  451.  
  452. \----------------------------------------------------------------------------*/
  453. void    MenuItemWindow :: SendSelectEvent( uchar num )
  454. {    
  455.     AEMenuWhere    blarg;
  456.     
  457.     blarg.which = type;
  458.     blarg.num = num;
  459.     
  460.     AH.SendEventToCurrentApp( kAEMenuSelect , (void *)&blarg );
  461. }
  462.  
  463. /*----------------------------------------------------------------------------\
  464.  
  465.     MenuItemWindow :: AddItemToUpdate
  466.  
  467. \----------------------------------------------------------------------------*/
  468. void    MenuItemWindow :: AddItemToUpdate( uchar which )
  469. {    
  470.     rect    loc;
  471.     
  472.     loc.left = screenLoc.left;
  473.     loc.top = screenLoc.top + which * kMenuItemHeight;
  474.     loc.right = loc.left + 200;
  475.     loc.bottom = loc.top + kMenuItemHeight;
  476.     
  477.     screen.AddRectToUpdate( loc );
  478. }